package com.goods.book.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.book.service.BookService;
import com.goods.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

//    获取查询参数生成url
    private String mekeURL(Book book){
        StringBuffer url=new StringBuffer("");
        if (book.getCid()!=null){
            url.append("&cid=").append(book.getCid());
        }
        if (book.getAuthor()!=null){
            url.append("&author=").append(book.getAuthor());
        }
        if (book.getBname()!=null){
            url.append("&bname=").append(book.getBname());
        }
        if (book.getPress()!=null){
            url.append("&press=").append(book.getPress());
        }
        return url.toString();
    }

//    @RequestMapping("SelectBooksByCase")
    public String selectBooksByCase(Book book, Model model){
        List<Book> books=bookService.selectBooksByCase(book);
        if (books.size()==1){
            model.addAttribute("book",books.get(0));
            return "jsps/book/desc";
        }else {
            model.addAttribute("books",books);
            return "jsps/book/list";
        }
    }

    @RequestMapping("SelectBooksByCase")
    public String selectBooksByCasePage(
            @RequestParam(defaultValue = "1")Integer pageNum,
            Book book, Model model){
//        获取查询参数，以复用
        String url=mekeURL(book);

        PageHelper.startPage(pageNum,8);
        List<Book> books=bookService.selectBooksByCasePage(book);
        PageInfo<Book> pageInfo=new PageInfo<Book>(books);


        if (books.size()==1){
            model.addAttribute("book",books.get(0));
            return "jsps/book/desc";
        }else {
            model.addAttribute("url",url.toString());
            model.addAttribute("pageInfo",pageInfo);
            return "jsps/book/list";
        }
    }

    @RequestMapping("AdminSelectBooksByCase")
    public String adminSelectBooksByCasePage(
            @RequestParam(defaultValue = "1")Integer pageNum,
            Book book, Model model){
//        获取查询参数，以复用
        String url=mekeURL(book);

        PageHelper.startPage(pageNum,10);
        List<Book> books=bookService.selectBooksByCasePage(book);
        PageInfo<Book> pageInfo=new PageInfo<Book>(books);


        if (books.size()==1){
            model.addAttribute("book",books.get(0));
            return "adminjsps/admin/book/desc";
        }else {
            model.addAttribute("url",url.toString());
            model.addAttribute("pageInfo",pageInfo);
            return "adminjsps/admin/book/list";
        }
    }

    @RequestMapping("/AdminAddBook")
    public String adminAddBook(Book book){
        return "";
    }


}
